package com.sz.virtualbank.service.notification.impl;

import com.sz.virtualbank.common.exception.BusinessException;
import com.sz.virtualbank.dao.entity.Notification;
import com.sz.virtualbank.dao.entity.User;
import com.sz.virtualbank.dao.mapper.NotificationMapper;
import com.sz.virtualbank.dao.mapper.UserMapper;
import com.sz.virtualbank.service.notification.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知消息服务实现类
 * 
 * @author Claude
 */
@Service
@Slf4j
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;
    
    @Autowired
    private UserMapper userMapper;

    @Override
    public Notification createNotification(Long userId, String title, String content, Integer type) {
        // 验证用户是否存在
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new BusinessException("用户不存在");
        }
        
        // 创建通知消息
        Notification notification = new Notification();
        notification.setUserId(userId);
        notification.setTitle(title);
        notification.setContent(content);
        notification.setType(type);
        notification.setStatus(0); // 未读
        notification.setCreateTime(LocalDateTime.now());
        notification.setUpdateTime(LocalDateTime.now());
        
        notificationMapper.insert(notification);
        
        return notification;
    }

    @Override
    public Map<String, Object> getNotificationList(Long userId, Integer type, Integer status, Integer page, Integer size) {
        if (page == null || page < 1) {
            page = 1;
        }
        
        if (size == null || size < 1) {
            size = 10;
        }
        
        int offset = (page - 1) * size;
        
        // 查询消息列表
        List<Notification> notifications = notificationMapper.selectByUserId(userId, type, status, offset, size);
        
        // 查询总记录数
        int total = notificationMapper.countByUserId(userId, type, status);
        
        Map<String, Object> result = new HashMap<>();
        result.put("total", total);
        result.put("list", notifications);
        
        return result;
    }

    @Override
    public Notification getNotificationDetail(Long id, Long userId) {
        Notification notification = notificationMapper.selectById(id);
        
        // 验证通知消息是否存在及归属权
        if (notification == null || !notification.getUserId().equals(userId)) {
            throw new BusinessException("通知消息不存在或不属于当前用户");
        }
        
        // 更新为已读
        if (notification.getStatus() == 0) {
            markAsRead(id, userId);
            notification.setStatus(1); // 已读
        }
        
        return notification;
    }

    @Override
    public boolean markAsRead(Long id, Long userId) {
        return notificationMapper.markAsRead(id, userId) > 0;
    }

    @Override
    public boolean markAllAsRead(Long userId) {
        return notificationMapper.markAllAsRead(userId) > 0;
    }

    @Override
    public boolean deleteNotification(Long id, Long userId) {
        return notificationMapper.deleteById(id, userId) > 0;
    }

    @Override
    public int countUnreadNotifications(Long userId) {
        return notificationMapper.countUnread(userId);
    }

    @Override
    public Notification sendSystemMessage(Long userId, String title, String content) {
        return createNotification(userId, title, content, 1); // 系统消息
    }

    @Override
    public Notification sendTransactionNotification(Long userId, String title, String content) {
        return createNotification(userId, title, content, 2); // 交易通知
    }

    @Override
    public Notification sendActivityMessage(Long userId, String title, String content) {
        return createNotification(userId, title, content, 3); // 活动消息
    }
}
